package com.zlm.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @Author: 郑
 *
 */
public class UdpReceiveDemo2 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(9090);
        DatagramPacket dataPacket = null;
        boolean flag = true;
        while (flag) {
            //准备数据包
            byte[] bytes = new byte[1024];
            dataPacket = new DatagramPacket(bytes, 0, bytes.length);
            socket.receive(dataPacket);

            byte[] data = dataPacket.getData();
            String receiveData = new String(data, 0, data.length);

            System.out.println(receiveData);
            //必须要去空格
            if(receiveData.trim().equals("bye")){
                flag = false;
            }

        }

        socket.close();

    }
}
